<template>
  <organize-form @save="handleSave" @cancel="$emit('cancel')" />
</template>

<script>
import { mapActions } from 'vuex'
import { addBusinessAgency, addSchoolAgency } from '@/api/organize'
import { isAgency } from './components/options.js'
import OrganizeForm from './components/organizeForm'

export default {
  name: 'OrganizeJoin',

  created() {
    this.GetOrganizeTypeOptions()
    this.GetAreaCascadeOptions()
    this.GetUniversityLabelOptions()
  },

  methods: {
    // 映射 Actions
    ...mapActions(['GetAreaCascadeOptions', 'GetOrganizeTypeOptions', 'GetUniversityLabelOptions']),

    // 增加机构
    async handleSave(params) {
      const { type } = params
      let data = null
      if (isAgency(type)) {
        data = await addBusinessAgency(params)
      } else {
        data = await addSchoolAgency(params)
      }
      if (data) {
        this.$message({
          message: '添加机构成功！',
          type: 'success'
        })
        this.$router.push('/organize/list')
      }
    }
  },

  components: {
    OrganizeForm
  }
}
</script>


